package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.dto.NursingProjectPageQueryDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 分页条件查询护理项目
     *
     * @param dto
     * @return
     */
    @Override
    public PageBean<NursingProjectVo> pageQuery(NursingProjectPageQueryDto dto) {
        //开启分页查询
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        //条件查询
        Page<NursingProjectVo> page = nursingProjectMapper.pageQuery(dto);
        //通过PageBean的of方法快速构建一个对象
        return PageBean.of(page, NursingProjectVo.class);
    }

    @Override
    public void addNursingProject(NursingProjectDto dto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(dto, nursingProject);
        nursingProjectMapper.insert(nursingProject);
    }

    @Override
    public NursingProjectVo read(Long id) {
        NursingProject nursingProject = new NursingProject();
        nursingProject = nursingProjectMapper.selectById(id);
        NursingProjectVo nursingProjectVo = new NursingProjectVo();
        BeanUtils.copyProperties(nursingProject, nursingProjectVo);
        return nursingProjectVo;
    }

    @Override
    public void updateNursingProject(NursingProjectDto dto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(dto, nursingProject);
        nursingProjectMapper.updateById(nursingProject);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        nursingProjectMapper.updateStatus(id, status);
    }

    @Override
    public void delete(Long id) {
        nursingProjectMapper.deleteById(id);
    }

    @Override
    @Cacheable(cacheNames = "ZZYL_NursingProject", key = "'LIST'")
    public List<NursingProject> getAll() {
        return nursingProjectMapper.getAll();
    }
}
